﻿using Bit.Art.Core.Models;
using Microsoft.EntityFrameworkCore;

namespace Bit.Art.Db
{
    /// <summary>
    /// 保存交易数据相关的数据库，需要支持分表分库
    /// </summary>
    // 数据库创建步骤：
    // 1. 只运行一次：
    //      dotnet ef migrations add v1 -c ExchangeDb -o Data/Migrations/ExchangeDbContext
    //      dotnet ef migrations add v1 -c ExchangeConfigDb -o Data/Migrations/ExchangeConfigDbContext
    // 2. 创建数据库
    //      dotnet ef database update -c ExchangeDb
    //      dotnet ef database update -c ExchangeConfigDb
    public class ExchangeDb : DbContext
    {
        public ExchangeDb() { }

        public ExchangeDb(DbContextOptions<ExchangeDb> options)
            : base(options)
        {
        }

        public DbSet<Order> Orders { get; set; }

        public DbSet<AccountBalance> AccountBalances { get; set; }

        public DbSet<WithdrawRequest> WithdrawRequests { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //foreach (var property in modelBuilder.Model.GetEntityTypes()
            //    .SelectMany(t => t.GetProperties())
            //    .Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
            //{
            //    property.Relational().ColumnType = "decimal(16, 8)";
            //}
            // TODO: 需要考虑下单价格和数量的数字位数限制！
            // 具体的任务参考任务：https://trello.com/c/lbHfqhjd/39-%E6%92%AE%E5%90%88%E5%BC%95%E6%93%8E%E5%BC%82%E5%B8%B8%E6%83%85%E5%86%B5%E6%96%B9%E9%9D%A2%E7%9A%84%E5%8A%9F%E8%83%BD%E6%B5%8B%E8%AF%95
            modelBuilder.Entity<Order>().Property(o => o.Price).HasColumnType("decimal(16, 8)");
            modelBuilder.Entity<Order>().Property(o => o.Volume).HasColumnType("decimal(18, 8)");
            modelBuilder.Entity<Order>().Ignore(o => o.ClientIdentity)
                .Ignore(o => o.CurrentDealed)
                .Ignore(o => o.CurrentDealedQuote);
            modelBuilder.Entity<Order>().Property(o => o.VolumeRemaining).HasColumnType("decimal(18, 8)");
            modelBuilder.Entity<Order>().Property(o => o.LockedBalance).HasColumnType("decimal(18, 8)");
            modelBuilder.Entity<Order>().Property(o => o.QuoteVolumeDealed).HasColumnType("decimal(18, 8)");
            modelBuilder.Entity<Order>().Property(o => o.QuoteVolume).HasColumnType("decimal(18, 8)");
            modelBuilder.Entity<Order>().Property(o => o.Market).HasMaxLength(20);
            modelBuilder.Entity<AccountBalance>().Property(o => o.Currency).HasMaxLength(10);
            modelBuilder.Entity<AccountBalance>().Property(a => a.Balance).HasColumnType("decimal(18, 8)");
            modelBuilder.Entity<AccountBalance>().Property(a => a.Pending).HasColumnType("decimal(18, 8)");
            modelBuilder.Entity<AccountBalance>().Ignore(a => a.Available);
            modelBuilder.Entity<AccountBalance>().Property(a => a.Address).HasMaxLength(64);
            modelBuilder.Entity<AccountBalance>().Property(a => a.Tag).HasMaxLength(64);

            modelBuilder.Entity<WithdrawRequest>().Property(o => o.Coin).HasMaxLength(10).IsRequired();
            modelBuilder.Entity<WithdrawRequest>().Property(o => o.LastModifiedBy).HasMaxLength(30);
            modelBuilder.Entity<WithdrawRequest>().Property(o => o.TxId).HasMaxLength(100).IsRequired();
            modelBuilder.Entity<WithdrawRequest>().Property(o => o.Description).HasMaxLength(200);
            modelBuilder.Entity<WithdrawRequest>().Property(o => o.Amount).HasColumnType("decimal(18, 8)").IsRequired();
            
            modelBuilder.Entity<DepositHistory>().Property(o => o.Coin).HasMaxLength(10);
            modelBuilder.Entity<DepositHistory>().Property(o => o.CreatedBy).HasMaxLength(30).IsRequired();
            modelBuilder.Entity<DepositHistory>().Property(o => o.TxId).HasMaxLength(100).IsRequired();
            modelBuilder.Entity<DepositHistory>().Property(o => o.Tag).HasMaxLength(64);
            modelBuilder.Entity<DepositHistory>().Property(o => o.Description).HasMaxLength(200);
            modelBuilder.Entity<DepositHistory>().Property(o => o.CreatedBy).HasMaxLength(30);
            modelBuilder.Entity<DepositHistory>().Property(o => o.Amount).HasColumnType("decimal(18, 8)").IsRequired();
        }
    }
}
